package com.ql.gamelogm.dao.impl;

import com.ql.gamelogm.dao.DataLogDao;
import com.ql.gamelogm.entities.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Repository
public class DataLogDaoImpl implements DataLogDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<DataLog> findAllDataLogs() {
        String sql="select * from gamedata";
        List<DataLog> dataLogList=jdbcTemplate.query(sql, (resultSet, rowNum) -> new DataLog(
                resultSet.getInt("id"),
                resultSet.getLong("gamerid"),
                resultSet.getLong("actionid"),
                resultSet.getString("map"),
                resultSet.getLong("mapx"),
                resultSet.getLong("mapy"),
                resultSet.getString("gamername"),
                resultSet.getString("itemname"),
                resultSet.getLong("itemid"),
                resultSet.getString("tradeto"),
                resultSet.getString("tradefrom"),
                resultSet.getTimestamp("date")
        ));
        return dataLogList;
    }

    @Override
    public List<ProblemData> findProblemLogs() {

        String sql="select * from analysis";
        List<Analysis> analysisList=jdbcTemplate.query(sql,(resultSet, rowNum) -> new Analysis(
                resultSet.getTimestamp("recent")
        ));
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //最近分析时间
        String date=simpleDateFormat.format(analysisList.get(0).getRecentTime());

        Date curdate = new Date(System.currentTimeMillis());
        String date1=simpleDateFormat.format(curdate);
        /*用户嗑药速度*/
        String sql1="select gamername,itemname,date,count(*) from gamedata where date between '"+date+"'"+" and '"+date1+"'"+"group by gamername,date";
        List<UseitemCount> useitemCountList = jdbcTemplate.query(sql1, (resultSet, rowNum) -> new UseitemCount(
                resultSet.getString("gamername"),
                resultSet.getString("itemname"),
                resultSet.getTimestamp("date"),
                resultSet.getInt("count(*)")
        ));
        List<UseitemCount> useitemCountList1=new ArrayList<UseitemCount>();
        for(int i=0;i<useitemCountList.size();i++){
            if(useitemCountList.get(i).getItemname().contains("药")){
                useitemCountList1.add(useitemCountList.get(i));
            }
        }
        List<ProblemData> problemDataList=new ArrayList<ProblemData>();

        for(int i=0;i<useitemCountList1.size();i++){
            int rate=useitemCountList1.get(i).getUseCount();
            Date date2=useitemCountList1.get(i).getDate();
            //设定一秒五次嗑药速度为正常阈值
            if(rate>5&&rate<=10){//警告
                ProblemData problemData=new ProblemData();
                problemData.setId(i);
                problemData.setGamername(useitemCountList1.get(i).getGamername());
                problemData.setMediCount(useitemCountList1.get(i).getUseCount());

                double avgUse=1.00/(useitemCountList1.get(i).getUseCount());
                BigDecimal b=new  BigDecimal(avgUse);
                double   f1=b.setScale(3,   BigDecimal.ROUND_HALF_UP).doubleValue();
                problemData.setAvgtime(f1);
                int r=rate-5;
                DecimalFormat df=new DecimalFormat("0.0");
                String format = df.format((double) r / 5);
                double k=Double.parseDouble(format);
                int ovrate1=(int)(k*100);
                problemData.setOverrate(ovrate1);

                problemData.setDegree("警告");
                problemData.setDescribe("人物嗑药速度警告异常");
                problemData.setDate(useitemCountList1.get(i).getDate());
                problemDataList.add(problemData);
            }
            if(rate>10){//严重
                ProblemData problemData1=new ProblemData();
                problemData1.setId(i);
                problemData1.setGamername(useitemCountList1.get(i).getGamername());
                problemData1.setMediCount(useitemCountList1.get(i).getUseCount());

                double avgUse=1.00/(useitemCountList1.get(i).getUseCount());
                BigDecimal b=new  BigDecimal(avgUse);
                double   f1=b.setScale(3,   BigDecimal.ROUND_HALF_UP).doubleValue();
                problemData1.setAvgtime(f1);
                int r=rate-5;
                DecimalFormat df=new DecimalFormat("0.0");
                String format = df.format((double) r / 5);
                double k=Double.parseDouble(format);
                int ovrate1=(int)(k*100);
                problemData1.setOverrate(ovrate1);

                problemData1.setDegree("严重");
                problemData1.setDescribe("人物嗑药速度严重异常");
                problemData1.setDate(useitemCountList1.get(i).getDate());
                problemDataList.add(problemData1);
            }
        }
        return problemDataList;
    }
}
